-- Copyright (c) 2025 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at:
--         http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

local lu = require 'luaunit'
local dump = require 'nvme.nvme_mi_protocol.nvme_mi_dump_log'
local file_sec = require 'utils.file'
local utils = require 'mc.utils'
local project_dir = os.getenv('PROJECT_DIR')

TEST_dump_nvmemi_info = {}

-- 测试dump日志
function TEST_dump_nvmemi_info:test_nvme_dump_info()
    local obj_info_path = project_dir .. '/test/unit/nvme.log'
    print(obj_info_path)
    local fp_w = file_sec.open_s(obj_info_path, 'w+')
    lu.assertNotIsNil(fp_w)

    local obj = {
        smart_log = {
            host_reads               = 69889,
            critical_warning         = 0,
            endu_grp_crit_warn_sumry = 0,
            critical_comp_time       = 0,
            avail_spare              = 100,
            thm_temp1_trans_count    = 0,
            power_on_hours           = 17,
            unsafe_shutdowns         = 1,
            temperature              = 307,
            warning_temp_time        = 2,
            ctrl_busy_time           = 0,
            power_cycles             = 52,
            data_units_read          = 2918,
            percent_used             = 1,
            thm_temp1_total_time     = 0,
            thm_temp2_total_time     = 308,
            thm_temp2_trans_count    = 0,
            num_err_log_entries      = 0,
            media_errors             = 0,
            temp_sensor1             = 0,
            temp_sensor2             = 0,
            temp_sensor3             = 0,
            temp_sensor4             = 0,
            temp_sensor5             = 0,
            temp_sensor6             = 0,
            temp_sensor7             = 0,
            temp_sensor8             = 0,
            host_writes              = 3286,
            data_units_written       = 51,
            spare_thresh             = 10
        },
        feature_identifiers = {
            [1] = 1
        },
        fw_log = {
            afi = 1,
            frs1 = tonumber('2500'),
            frs2 = tonumber('   ')
        },
        supported_log_pages = {
            [2] = 1
        },
        error_log = {
            [1] = {
                error_count = 0,
                sqid = 0,
                cmdid = 0,
                status_field = 0,
                parm_error_location = 0,
                lba = 0,
                nsid = 0,
                vsia = 0,
                trtype = 0,
                csi = 0,
                trtype_spec_info = 0
            }
        },
        controller_health_status = {},
        mctp_trans_unit_size = {},
        nvm_subsystem_info = {},
        subsys_health_status = {}
    }
    dump.dump_log(obj, fp_w)
    fp_w:close()

    local file = file_sec.open_s(obj_info_path, "r")
    lu.assertNotIsNil(file)
    local content = utils.close(file, pcall(file.read, file, "*a"))
    lu.assertNotIsNil(content)
    -- 检查信息
    lu.assertNotEquals(string.find(content, 'available_spare_threshold'), nil)
    lu.assertEquals(string.find(content, 'temp_sensor4'), nil)
    lu.assertNotEquals(string.find(content, 'Error Log'), nil)
    lu.assertNotEquals(string.find(content, 'The command completed successfully'), nil)
    lu.assertNotEquals(string.find(content, 'trtype_spec_info: 0'), nil)
    lu.assertNotEquals(string.find(content, 'NAMESPACE SCOPE-'), nil)
    lu.assertNotEquals(string.find(content, 'frs1 : 2500'), nil)

    utils.remove_file(obj_info_path)
end